﻿using System.Data;
using System.Data.SqlClient;
using CoreLibrary.Template;

namespace CoreLibrary.Sql.Command {

    public class BackupCommand : ISqlCommand<bool,bool> {
        
        private readonly string _dbName;
        private readonly string _filePath;
        private readonly string _mediaName;
        
        const string SQL = @"
                USE ${databaseName};
                GO
                BACKUP DATABASE ${databaseName}
                TO DISK = '${filePath}'
                WITH FORMAT, MEDIANAME = '${mediaName}'
                GO
        ";

        public BackupCommand(string databaseName, string filePath, string mediaName) {
            _dbName   = databaseName;
            _filePath = filePath;
            _mediaName = mediaName;
        }

        public string GetSQL() {
            var parameters = new TemplateParameters();
            parameters.Put("databaseName", Util.GetString(_dbName));
            parameters.Put("filePath",     Util.GetString(_filePath));
            parameters.Put("mediaName",    Util.GetString(_mediaName));

            return SimpleTemplate.Load(SQL)
                                 .Generate(parameters);
        }

        public void SetParameters(SqlCommand command) {
            command.CommandType = CommandType.Text;
        }

        public bool GetResult(SqlConnection conn, bool result) {
            return true;
        }

    }
}